/*
 * Copyright (c) 2016 Mobvoi Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.umeox.moto.watch.themes.util;

import android.content.Context;
import android.content.res.Resources.Theme;
import android.content.res.TypedArray;
import android.os.Build;
import android.os.Environment;
import android.util.Log;
import android.util.TypedValue;

import com.umeox.moto.watch.themes.R;

import java.io.File;

public class ThemeUtils {

    public static final String SKIN = "Android/data/AppThemeSkin";
    public static final String SKIN1 = "skin/AppThemeSkin";

    private static TypedValue value;

    private static int getColor(Context context, int id, int defaultValue) {
        if (value == null) {
            value = new TypedValue();
        }

        try {
            Theme theme = context.getTheme();
            if (theme != null && theme.resolveAttribute(id, value, true)) {
                if (value.type >= TypedValue.TYPE_FIRST_INT && value.type <= TypedValue.TYPE_LAST_INT) {
                    return value.data;
                } else if (value.type == TypedValue.TYPE_STRING) {
                    return context.getResources().getColor(value.resourceId);
                }
            }
        } catch (Exception ignored) {
        }

        return defaultValue;
    }

    public static int windowBackground(Context context, int defaultValue) {
        return getColor(context, android.R.attr.windowBackground, defaultValue);
    }

    public static int textColorPrimary(Context context, int defaultValue) {
        return getColor(context, android.R.attr.textColorPrimary, defaultValue);
    }

    public static int textColorTertiary(Context context, int defaultValue) {
        return getColor(context, android.R.attr.textColorTertiary, defaultValue);
    }

    public static int textColorHighlight(Context context, int defaultValue) {
        return getColor(context, android.R.attr.textColorHighlight, defaultValue);
    }

    public static int colorPrimary(Context context, int defaultValue) {
        return getColor(context, R.attr.u_colorPrimary, defaultValue);
    }

    public static int colorPrimaryDark(Context context, int defaultValue) {
        return getColor(context, android.R.attr.colorPrimaryDark, defaultValue);
    }

    public static int colorAccent(Context context, int defaultValue) {
        return getColor(context, R.attr.u_colorAccent, defaultValue);
    }

    public static int colorControlNormal(Context context, int defaultValue) {
        return getColor(context, R.attr.u_colorControlNormal, defaultValue);
    }

    public static int colorControlActivated(Context context, int defaultValue) {
        return getColor(context, R.attr.u_colorControlActivated, defaultValue);
    }

    public static int colorControlHighlight(Context context, int defaultValue) {
        return getColor(context, R.attr.u_colorControlHighlight, defaultValue);
    }

    public static int colorButtonNormal(Context context, int defaultValue) {
        return getColor(context, R.attr.u_colorButtonNormal, defaultValue);
    }

    public static int getType(TypedArray array, int index) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            return array.getType(index);
        } else {
            TypedValue value = array.peekValue(index);
            return value == null ? TypedValue.TYPE_NULL : value.type;
        }
    }

    public static CharSequence getString(TypedArray array, int index, CharSequence defaultValue) {
        String result = array.getString(index);
        return result == null ? defaultValue : result;
    }


    /**
     * 获取皮肤资源路径
     *
     * @return
     */
    public static File getSkinPath() {
        final File directory = Environment.getExternalStorageDirectory();
        File dir = new File(directory, SKIN);
        boolean folder = true;
        if (!dir.exists()) {
            folder = dir.mkdirs();
        }
        Log.d("Theme", "getSkinPath " + dir.getAbsolutePath() + " " + folder);
        return folder ? dir : null;
    }

    /**
     * 获取皮肤资源路径
     *
     * @return
     */
    public static File getMusicPath() {
        final File directory = Environment.getExternalStorageDirectory();
        File dir = new File(directory, "Music");
        boolean folder = true;
        if (!dir.exists()) {
            folder = dir.mkdirs();
        }
        return folder ? dir : null;
    }
}